package com.vanvalt.payhui.modules.pay.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.vanvalt.payhui.modules.pay.entity.Banner;
import com.vanvalt.payhui.modules.pay.entity.News;
import com.vanvalt.payhui.modules.pay.entity.Pos;
import com.vanvalt.payhui.modules.pay.service.BannerService;
import com.vanvalt.payhui.modules.pay.service.NewsService;
import com.vanvalt.payhui.modules.pay.service.PosService;
import com.vanvalt.payhui.util.constat.Constant;

/**
 * Web站点
 * @author Xiaoyang
 *
 */
@Controller
@RequestMapping(value = "${frontPath}/site")
public class WebController extends BaseController {

	@Autowired
	private NewsService newsService;
	@Autowired
	private BannerService bannerService;
	@Autowired
	private PosService posService;
	
	/**
	 * 网站首页
	 */
	@RequestMapping
	public String index(Model model) {
		
		// Banner图
		List<Banner> banners = this.getBannerList();
		
		// 新闻资讯
		List<Map<String, Object>> newsMapList = this.getNewsList(4);
		
		// 产品-POS机
		List<Map<String, Object>> posBigMapList = this.getPosList(Constant.POS_TYPE_BIG, 3);
		// 产品-手刷
		List<Map<String, Object>> posSmallMapList = this.getPosList(Constant.POS_TYPE_SMALL, 3);
		
		model.addAttribute("banners", banners);
		model.addAttribute("news", newsMapList);
		model.addAttribute("posBigMapList", posBigMapList);
		model.addAttribute("posSmallMapList", posSmallMapList);
		model.addAttribute("isIndex", true);
		
		return "site/index";
	}
	
	/**
	 * Banner图列表
	 * @return
	 */
	private List<Banner> getBannerList(){
		
		List<Banner> banners = Lists.newArrayList();
		
		Banner bannerEntity = new Banner();
		bannerEntity.setDelFlag(Constant.DEL_FLAG_NORMAL);
		bannerEntity.setType(Constant.BANNER_TYPE_PC);
		
		banners = bannerService.findList(bannerEntity);
		
		String baseUrl = Global.getConfig("web.file.download.url");
		
		for(Banner banner:banners){
			String imageUrl = banner.getImage();
			imageUrl = baseUrl + imageUrl;
			
			banner.setImage(imageUrl);
		}
		
		return banners;
	}
	
	/**
	 * 新闻资讯列表
	 * @param maxSize
	 * @return
	 */
	private List<Map<String, Object>> getNewsList(int maxSize){
		
		List<Map<String, Object>> newsMapList = Lists.newArrayList();
		
		News newsParam = new News();
		newsParam.setDelFlag(Constant.DEL_FLAG_NORMAL);
		
		List<News> newsList = newsService.findList(newsParam);
		// 默认取前4条记录
		int newsSize = newsList.size();
		if(newsSize > maxSize){
			newsList = newsList.subList(0, maxSize);
		}
		
		String baseUrl = Global.getConfig("web.file.download.url");
		
		for(News news:newsList){
			
			Map<String,Object> newsMap = Maps.newHashMap();
			newsMap.put("title", news.getTitle());
			newsMap.put("comment", news.getComment());
			newsMap.put("link", news.getLink());
			
			String images = news.getImage();
			String imageUrl = this.getPictureUrl(images, baseUrl);
			newsMap.put("imageUrl", imageUrl);
			
			Date createDate = news.getCreateDate();
			String month = createDate.getMonth()+1+"月";
			String day = DateUtils.formatDate(createDate, "dd");
			
			newsMap.put("month", month);
			newsMap.put("day", day);
			newsMap.put("createDate", DateUtils.formatDate(createDate, "yyyy-MM-dd"));
			
			newsMapList.add(newsMap);
		}
		
		return newsMapList;
	}
	
	/**
	 * POS机/手刷列表
	 * @return
	 */
	private List<Map<String, Object>> getPosList(String type, int maxSize){
		
		List<Map<String, Object>> posMapList = Lists.newArrayList();
	
		List<Pos> posList = Lists.newArrayList();
		
		Pos posEntity = new Pos();
		posEntity.setDelFlag(Constant.DEL_FLAG_NORMAL);
		posEntity.setType(type);
		
		posList = posService.findList(posEntity);
		
		int size = posList.size();
		// 每类POS机默认取3条
		if(size > maxSize){
			posList = posList.subList(0, maxSize);
		}
		
		String baseUrl = Global.getConfig("web.file.download.url");
		
		for(Pos pos:posList){
			
			Map<String, Object> posMap = Maps.newHashMap();
			
			posMap.put("id", pos.getId());
			posMap.put("name", pos.getName());
			
			String pictures = pos.getPictures();
			String pictureUrl = this.getPictureUrl(pictures, baseUrl);
			
			posMap.put("pictureUrl", pictureUrl);
			posMap.put("type", pos.getType());
			
			posMapList.add(posMap);
		}
		
		return posMapList;
	}
	
	private String getPictureUrl(String pictures, String baseUrl){
		
		String pictureUrl = "";
		
		List<String> resultList = Lists.newArrayList();
		
		if(pictures != null && !"".equals(pictures)){
			if(pictures.contains(Constant.SPLIT_FILE_NAME)){
				String[] pictureArray = pictures.split("\\|");
				
				for(String pic:pictureArray){
					if(pic.trim() != null && !"".equals(pic.trim())){
						pic = baseUrl + pic;
						resultList.add(pic);
					}
				}
			}
		}
		// 默认取第一张图片
		pictureUrl =resultList.get(0);
		
		return pictureUrl;
	}
	
	/**
	 * POS机
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "pos")
	public String pos(@RequestParam(required=false) String id, Model model) {
		
		// 产品-POS机
		List<Map<String, Object>> posBigMapList = this.getPosList(Constant.POS_TYPE_BIG, 6);
		// 产品-手刷
		List<Map<String, Object>> posSmallMapList = this.getPosList(Constant.POS_TYPE_SMALL, 6);
		
		model.addAttribute("posBigMapList", posBigMapList);
		model.addAttribute("posSmallMapList", posSmallMapList);
				
		return "site/pos";
	}
	
	/**
	 * 相关下载
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "download")
	public String download(@RequestParam(required=false) String id, Model model) {
		
		return "site/download";
	}
	
	/**
	 * 第三方客服
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "cs3")
	public String customerService3rd(@RequestParam(required=false) String id, Model model) {
		
		return "site/customerService3rd";
	}
	
	/**
	 * 联系我们
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "contact")
	public String contact(@RequestParam(required=false) String id, Model model) {
		
		return "site/contact";
	}
	
	/**
	 * 企业文化
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "culture")
	public String culture(@RequestParam(required=false) String id, Model model) {
		
		return "site/culture";
	}
	
	/**
	 * 常见问题
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "faq")
	public String faq(@RequestParam(required=false) String id, Model model) {
		
		return "site/faq";
	}
	
	/**
	 * 行业新闻
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "news")
	public String news(@RequestParam(required=false) String id, Model model) {
		
		List<Map<String, Object>> newsMapList = this.getNewsList(6);
		model.addAttribute("news", newsMapList);
		
		return "site/news";
	}
	
	/**
	 * 企业介绍
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "introduction")
	public String introduction(@RequestParam(required=false) String id, Model model) {
		
		return "site/introduction";
	}
	
	/**
	 * 各地服务商
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "facilitator")
	public String facilitator(@RequestParam(required=false) String id, Model model) {
		
		return "site/facilitator";
	}
	
	/**
	 * 我要加盟
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "join")
	public String join(@RequestParam(required=false) String id, Model model) {
		
		return "site/join";
	}
	
}
